package com.lcc.lynx.jdbc;

import com.github.pagehelper.PageInfo;
import com.lcc.lynx.common.ResultVO;
import com.lcc.lynx.security.annotation.Permission;
import com.lcc.lynx.common.utils.StrUtil;
import com.lcc.lynx.jdbc.pojo.TablePojo;
import com.lcc.lynx.jdbc.pojo.PageParams;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public interface IBaseController<DV extends TablePojo> {

    /**
     * 获取service
     * @return
     */
    IBaseService<DV> getService();

    /**
     * 查询所有
     * @param dto
     * @return
     */
    @Permission(Permission.search)
    @RequestMapping("/list")
    default ResultVO<List<DV>> list(DV dto){
        return ResultVO.success(getService().getMapper().select(dto));
    }

    /**
     * 分页查询
     * @param dto
     * @param pageParams
     * @return
     */
    @Permission(Permission.search)
    @RequestMapping("/page")
    default ResultVO<PageInfo<DV>> page(DV dto, PageParams pageParams){
        return ResultVO.success(getService().selectPage(dto, pageParams));
    }

    /**
     * 新增
     * @param dto
     * @return
     */
    @Permission(Permission.add)
    @RequestMapping("/insert")
    default ResultVO<?> insert(DV dto){
        if(dto == null){
            return ResultVO.fail("新增对象不能为空");
        }
        int i = getService().insert(dto);
        return i == 0 ? ResultVO.fail("新增失败") : ResultVO.success();
    }

    /**
     * 新增
     * @param dvList
     * @return
     */
    @Permission(Permission.add)
    @RequestMapping("/insertList")
    default ResultVO<?> insertList(@RequestBody List<DV> dvList){
        if(StrUtil.isEmpty(dvList)){
            return ResultVO.fail("新增对象列表不能为空");
        }
        int i = getService().insertList(dvList);
        return i == 0 ? ResultVO.fail("批量新增失败") : ResultVO.success();
    }

}
